// ////////////////////////////////////////////////////////////////////////////////////////
// NAME: AppManager.java 
// AUTHOR: CDT Riley Mckenna 
// LAST MODIFIED: 28 March 2012 
// DESCRIPTION: Contains code for the settings Tab of Nullwall
// LIMITATIONS: 
// CITATIONS: In code
// 
// ////////////////////////////////////////////////////////////////////////////////////////

package prototype.revision;

import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import com.stericson.RootTools.RootTools;
import com.stericson.RootTools.RootToolsException;

public class AppManager {

	ArrayList<String> Apps = new ArrayList<String>();
	//private FileOperations fo = new FileOperations(); 

	protected void exec(String Perm) {
		final String path = "/data/system/packages.xml";
		String perm = Perm;
		String tmp = "";

		try {
			RootTools.sendShell("chmod 0777 /data/system/packages.xml", 1000);
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (RootToolsException e2) {
			e2.printStackTrace();
		} catch (TimeoutException e2) {
			e2.printStackTrace();
		}

		// reset the array of App Names
		for (int i = 0; i < Apps.size(); i++) {
			Apps.remove(i);
		}

		try {
			LineNumberReader lnr = new LineNumberReader(new FileReader(path));
			String line;
			while ((line = lnr.readLine()) != null) {
				if (line.contains("<shared-user")) {
					tmp = line;
				}
				if (line.contains("<perms>")) {
					while ((line = lnr.readLine()) != null) {
						if (line.contains("</perms>")) {
							break;
						}
						if (line.contains(perm)) {
							Apps.add(tmp);
						}
					}
				}
			}
			lnr.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}